﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Luna.Framework;
using System.Windows.Forms;

namespace Luna
{
	public class LunaApplication: IContextAwareObject
	{
		private IAppContext context;

		public AppArgument Arguments { get; private set; }

		public IAppContext Context {
			get { return context; }
			set {
				if (context == null) {
					context = value;
					value.Host = this;
				}
			}
		}

		public Form MainForm { get; set; }

		public virtual void run() {
			if (MainForm != null)
				Application.Run(MainForm);
		}

		public static void runApplication<T>(string[] args, string manifestFile, string appId = "luna.application") where T: LunaApplication {
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);

			AppArgument arg = new AppArgument(args);
			string file = arg.getFreeParamAfterSwitch("manifest", manifestFile);

			Framework.Framework.initialize(file);
			LunaApplication app = Framework.Framework.createApplication<T>(appId, null);
			
			app.Arguments = arg;
			app.run();
		}
	}
}
